﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;

using Warehouse.Service.Data;
using Warehouse.Silverlight.Core.Infrastructure.ViewModels;
using Warehouse.Silverlight.Modules.Main.Models;
using Warehouse.Silverlight.Modules.Main.Services;

namespace Warehouse.Silverlight.Modules.Main.ViewModels
{
    [Export]
    public class CategoryTreeViewModel : ViewModel
    {
        private const string _title = "Category";
        private readonly ICategoryService _categoryService;
        private readonly IEventAggregator _eventAggregator;
        private ObservableCollection<Category> _items;
        private Category _selectedItem;

        [ImportingConstructor]
        public CategoryTreeViewModel(ICategoryService categoryService, IEventAggregator eventAggregator)
		{
            _categoryService = categoryService;
            _eventAggregator = eventAggregator;

            Initialize();
		}

        public string Title { get { return _title; } }

        public ObservableCollection<Category> Items { get { return _items; } }

        public Category SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    RaisePropertyChanged(() => SelectedItem);

                    if (_selectedItem != null)
                    {
                        var e = _eventAggregator.GetEvent<SelectedCategoryChangedEvent>();
                        e.Publish(_selectedItem.CategoryId);
                    }
                }
            }
        }

        //public ICommand SelectionChangedCommand { get; private set; }

        // Override
        protected override void Initialize()
        {
            //SetupCommands();

            _items = _categoryService.GetAllCategories();
        }

        #region private Helpers

        //private void SetupCommands()
        //{
        //    SelectionChangedCommand = new DelegateCommand<SelectionChangedEventArgs>(
        //        (e) => this.OnSelectionChangedCommand(e));
        //}

        //private void OnSelectionChangedCommand(SelectionChangedEventArgs e)
        //{
        //    if (e.AddedItems.Count == 1)
        //    {
        //        var enumerator = e.AddedItems.GetEnumerator();
        //        enumerator.MoveNext();
        //        var c = (Category)enumerator.Current;
        //    }
        //}

        #endregion
    }
}
